package com.gljx.web.controller.weixin;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Student;
import com.gljx.web.Entity.StudentInitiativeCar;
import com.gljx.web.service.SharecarsubService;
import com.gljx.web.service.StudentInitiativeCarService;
import com.gljx.web.service.StudentService;

/**
 * Created by Administrator on 2018/1/12 0012.
 */
@Controller
@RequestMapping("/weixin/kmyy")
public class CalculateTimeController {

    //自动注入
    @Autowired
    private StudentService studentService;

    //自动注入
    @Autowired
    private StudentInitiativeCarService studentInitiativeCarService;

    //自动注入
    @Autowired
    private SharecarsubService sharecarsubService;

    @Autowired
    private HttpSession session;

    /**
     * @Author lcx
     * @Date 2018/1/12 0012 下午 2:37
     * 跳转到微信预约的界面主页面
     */
    @RequestMapping("/CalculateTimeMain")
    public String queryCalculateTime() {
        return "/weixin/CalculateTimeReservation/CalculateTimeMain";
    }

    /**
     * @Author lcx
     * @Date 2018/1/16 0016 下午 2:50
     * 跳转到选择科目的页面
     */
    @RequestMapping("/CalculateTimeKmyy")
    public String queryCalculateTimeAsKmyy(HttpServletRequest request, String fid, String kemu) {

        Student student = (Student) session.getAttribute("weixinStudent");

        //如果 >= 0 则是小车
        if (student.getCfslzjcxdh().compareTo("C1") >= 0) {
            //小车
            request.setAttribute("carTypeState", 1);
        } else {
            //大车
            request.setAttribute("carTypeState", 2);
        }

        //查询最新一条数据
        List<StudentInitiativeCar> studentInitiativeCarList = studentInitiativeCarService.queryShareCarSubByStudentId(fid);

        request.setAttribute("studentInitiativeCarList", studentInitiativeCarList);

        return "/weixin/CalculateTimeReservation/CalculateTimeAsKmyy";
    }

    /**
     * @Author lcx
     * @Date 2018/1/17 0017 上午 10:14
     * 跳转到练车时间选择界面
     * carType 1 大 2 小
     * cftime 0 否 1 是
     */
    @RequestMapping("/CalculateTimeLcss")
    public String queryCalculateTimeAsLcss(HttpServletRequest request, String subject, String cftime, String carType) {
        //获取当前时间
        String time = TimeFormat.timeFormat(new Date());
        //查询数据
        List<StudentInitiativeCar> studentInitiativeCars = studentInitiativeCarService.selectPracticeCarTime(time, subject, cftime, carType);
        //查询本次练车时间有多少人在练车
        for (StudentInitiativeCar studentInitiativeCar : studentInitiativeCars) {
            studentInitiativeCar.setYyrs(studentInitiativeCarService.queryOrderSum(studentInitiativeCar.getFid(), null));
        }
        session.setAttribute("kemu", subject);
        request.setAttribute("studentInitiativeCars", studentInitiativeCars);

        return "/weixin/CalculateTimeReservation/CalculateTimeAsLcss";

    }

    /**
     * @Author lcx
     * @Date 2018/1/17 0017 下午 1:46
     * 跳转到选择教练界面
     */
    @RequestMapping("/CalculateTimeXzjl")
    public String queryCalculateTimeAsXzjl(HttpServletRequest request, String lcsjId, String lcsjfid) {

        //查询数据
        List<StudentInitiativeCar> studentInitiativeCars = studentInitiativeCarService.queryCoach(lcsjId);
        //查询已预约教练的练车人数
        for (StudentInitiativeCar studentInitiativeCar : studentInitiativeCars) {
            studentInitiativeCar.setYyrs(studentInitiativeCarService.queryOrderSum(lcsjId, studentInitiativeCar.getCoachid()));
        }
        StudentInitiativeCar studentInitiativeCarTem = studentInitiativeCarService.selectPracticeCarTimeByFid(lcsjfid);
        session.setAttribute("lcsjId", lcsjId);
        session.setAttribute("lcsjfid", lcsjfid);
        session.setAttribute("stratTime", studentInitiativeCarTem.getStratTime());
        session.setAttribute("endTime", studentInitiativeCarTem.getEndTime());
        request.setAttribute("studentInitiativeCars", studentInitiativeCars);

        return "/weixin/CalculateTimeReservation/CalculateTimeAsXzjl";
    }

    /**
     * @Author lcx
     * @Date 2018/1/18 0018 下午 4:25
     * ajax 查询是否可以选择该练车时间
     */
    @RequestMapping("/selectPracticeCarTimeByInfo")
    @ResponseBody
    public boolean selectPracticeCarTimeByInfo(String lcsjfid) {
        //如果选中的练车时间是已经预约的则返回
        if (1 == studentInitiativeCarService.selectPracticeCarTimeByInfo(lcsjfid, ((Student) session.getAttribute("weixinStudent")).getFid())) {
            return false;
        }
        return true;
    }


    /**
     * wx根据身份证号查询学员
     *
     * @param request
     * @param cerno
     * @return
     */
    @RequestMapping("/queryStudentByIdCard")
    @ResponseBody
    public Student queryStudentByIdCard(HttpServletRequest request, String cerno) {
        Student student = studentService.queryStudentByIdCardNoWx(cerno);
        if (student == null) {

        } else {
            request.getSession().setAttribute("weixinStudent", student);
        }
        return student;
    }

    /**
     * 根据学生id查询最新的练车时间记录
     *
     * @param studentId
     * @return
     */
    @RequestMapping("/queryShareCarSub")
    @ResponseBody
    public List<StudentInitiativeCar> queryShareCarSub(String studentId) {
        //查询最新一条数据
        List<StudentInitiativeCar> studentInitiativeCarList = studentInitiativeCarService.queryShareCarSubByStudentId(studentId);
        return studentInitiativeCarList;
    }

    /**
     * 根据练车时间fid删除
     *
     * @param fid
     * @return
     */
    @RequestMapping("/deleteShareCarSubById")
    @ResponseBody
    public int deleteShareCarSubById(String fid) {
        int i = sharecarsubService.deleteBySharecarsubentryByFid(fid);
        return i;
    }

    /**
     * @Author lcx
     * @Date 2018/1/17 0017 下午 3:00
     * 添加微信预约报名学员
     */
    @RequestMapping(value = "/add_Sharecarsubentry")
    @ResponseBody
    public StudentInitiativeCar addSharecarsubAsWx(String coachid) {

        //获取学生信息
        Student student = (Student) session.getAttribute("weixinStudent");
        String kemu = (String) session.getAttribute("kemu");
        String lcsjId = (String) session.getAttribute("lcsjId");
        //获取选择的科目

        StudentInitiativeCar studentInitiativeCar = sharecarsubService.addsharecarsubAsWx(kemu, coachid, lcsjId, student);

        session.removeAttribute("kemu");
        session.removeAttribute("lcsjId");
        session.removeAttribute("stratTime");
        session.removeAttribute("endTime");

        return studentInitiativeCar;
    }


    /**
     * @Author lcx
     * @Date 2018/1/19 0019 上午 11:31
     *
     *
     * pc 端
     *
     */


    /**
     * pc进入
     *
     * @return
     */
    @RequestMapping("/goStudentPc")
    public String goStudentPc() {
        return "/weixin/CalculateTimeReservationPc/CalculateTimeMain";
    }

    /**
     * @Author lcx
     * @Date 2018/1/19 0019 上午 11:32
     */
    @RequestMapping("/CalculateTimeKmyyPc")
    public String queryCalculateTimeAsKmyyPc(HttpServletRequest request, String fid, String kemu) {

        Student student = (Student) session.getAttribute("weixinStudent");

        //如果 >= 0 则是小车
        if (student.getCfslzjcxdh().compareTo("C1") >= 0) {
            //小车
            request.setAttribute("carTypeState", 1);
        } else {
            //大车
            request.setAttribute("carTypeState", 2);
        }

        //查询最新一条数据
        List<StudentInitiativeCar> studentInitiativeCarList = studentInitiativeCarService.queryShareCarSubByStudentId(fid);

        request.setAttribute("studentInitiativeCarList", studentInitiativeCarList);
        request.setAttribute("cfficompanyName", student.getCfficompanyName());

        return "/weixin/CalculateTimeReservationPc/CalculateTimeAsKmyy";
    }

    /**
     * @Author lcx
     * @Date 2018/1/19 0019 上午 11:54
     */
    @RequestMapping("/CalculateTimeLcssPc")
    @ResponseBody
    public List<StudentInitiativeCar> queryCalculateTimeAsLcssPc(HttpServletRequest request, String subject, String cftime, String carType) {
        //获取当前时间
        String time = TimeFormat.timeFormat(new Date());
        //查询数据
        List<StudentInitiativeCar> studentInitiativeCars = studentInitiativeCarService.selectPracticeCarTime(time, subject, cftime, carType);
        //查询本次练车时间有多少人在练车
        for (StudentInitiativeCar studentInitiativeCar : studentInitiativeCars) {
            studentInitiativeCar.setYyrs(studentInitiativeCarService.queryOrderSum(studentInitiativeCar.getFid(), null));
        }
        session.setAttribute("kemu", subject);
        request.setAttribute("studentInitiativeCars", studentInitiativeCars);

        return studentInitiativeCars;

    }

    @RequestMapping("/CalculateTimeXzjlPc")
    @ResponseBody
    public List<StudentInitiativeCar> queryCalculateTimeAsXzjlPc(HttpServletRequest request, String lcsjId, String lcsjfid) {

        //查询数据
        List<StudentInitiativeCar> studentInitiativeCars = studentInitiativeCarService.queryCoach(lcsjId);
        //查询已预约教练的练车人数
        for (StudentInitiativeCar studentInitiativeCar : studentInitiativeCars) {
            studentInitiativeCar.setYyrs(studentInitiativeCarService.queryOrderSum(lcsjId, studentInitiativeCar.getCoachid()));
        }
        StudentInitiativeCar studentInitiativeCarTem = studentInitiativeCarService.selectPracticeCarTimeByFid(lcsjfid);
        session.setAttribute("lcsjId", lcsjId);
        session.setAttribute("lcsjfid", lcsjfid);
        session.setAttribute("stratTime", studentInitiativeCarTem.getStratTime());
        session.setAttribute("endTime", studentInitiativeCarTem.getEndTime());

        return studentInitiativeCars;
    }


}
